package one.bugu.android.demon.util;

import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class BaseViewHolder {
    private View inflate;
    private Context mContext;

    public BaseViewHolder(View itemView, Context context) {
        this.inflate = itemView;
        this.mContext = context;
    }

    /*设置TextView 控件*/
    public void setText(int tvID, String text, CallBack<TextView> callBack) {
        TextView tx = (TextView) inflate.findViewById(tvID);
        tx.setText(text);
        if (callBack != null) {
            callBack.run(tx);
        }
    }

    /*设置ImageView 控件*/
    public void setImageResource(int ivID, int resouceId, CallBack<ImageView> callBack) {
        ImageView img = (ImageView) inflate.findViewById(ivID);
        img.setImageResource(resouceId);
        if (callBack != null) {
            callBack.run(img);
        }
    }

    /*设置CheckBox 控件*/
    public void setCheckBox(int cbID, boolean cb, CallBack<CheckBox> callBack) {
        CheckBox cbox = (CheckBox) inflate.findViewById(cbID);
        cbox.setChecked(cb);
        if (callBack != null) {
            callBack.run(cbox);
        }
    }

    /*设置ImageButton 控件*/
    public void setImageButton(int ibtnID, CallBack<ImageButton> callBack) {
        ImageButton ibtn = (ImageButton) inflate.findViewById(ibtnID);
        if (callBack != null) {
            callBack.run(ibtn);
        }
    }

    /*设置LinearLayout 控件*/
    public void setLinearLayout(int layoutID, CallBack<LinearLayout> callBack) {
        LinearLayout layout = (LinearLayout) inflate.findViewById(layoutID);
        if (callBack != null) {
            callBack.run(layout);
        }
    }

    /*设置RelativeLayout 控件*/
    public void setRelativeLayout(int layoutID, CallBack<RelativeLayout> callBack) {
        RelativeLayout layout = (RelativeLayout) inflate.findViewById(layoutID);
        if (callBack != null) {
            callBack.run(layout);
        }
    }
    /*设置GridView 控件*/
    public void setGridView(int gridID, CallBack<GridView> callBack) {
        GridView grid = (GridView) inflate.findViewById(gridID);
        if (callBack != null) {
            callBack.run(grid);
        }
    }

    /*回调函数*/
    public interface CallBack<T> {
        void run(T t);
    }
}
